#include "myapp.h"
#include "im_dialog.h"

DECLARE_APP(MyApp);

BEGIN_EVENT_TABLE(ImportDialog, wxDialog)
	EVT_BUTTON(import_close,ImportDialog::im_CloseBtn_clicked)
	EVT_BUTTON(import_execute,ImportDialog::im_ExecuteBtn_clicked)
	EVT_BUTTON(import_fileref,ImportDialog::im_FileRefBtn_clicked)
	EVT_RADIOBOX(import_type,ImportDialog::im_ImportType_selected)
	EVT_CHECKBOX(import_userange,ImportDialog::im_ImportUseRange_selected)
	EVT_CHOICE(import_format,ImportDialog::im_ImportFormat_changed)
END_EVENT_TABLE() 

ImportDialog::ImportDialog(wxWindow *parent)
:wxDialog(parent, wxID_ANY, _("Import and Export"), wxDefaultPosition, wxDefaultSize)
{
	SetClientSize(wxSize(640,480));

	wxString import_items[] = {
		_("Import"),_("Export"),_("For other program")
	};

	wxString target_items[] = {
		_("Budgets"),_("Expenses and Items"),_("Accounts"),_("Periodical budgets")
	};
	wxString format_items[] = {
		_T("SQL insert"),_T("XML"),_T("CSV")
	};
	wxString delim_items[] = {
		_("Tab"),_("Comma")
	};
	im_ImportType = new wxRadioBox(this,import_type,_("Type"),wxDefaultPosition,wxDefaultSize,3,import_items,3,wxRA_SPECIFY_ROWS);
	wxStaticText *label1 = new wxStaticText(this,wxID_ANY,_("Target list:"));
	im_ImportDBList = new wxCheckListBox(this,wxID_ANY,wxDefaultPosition,wxSize(220,110),4,target_items);

	wxStaticBox *sbox = new wxStaticBox(this,wxID_ANY,_("Input base budget"));
	wxStaticBoxSizer *sbsizer = new wxStaticBoxSizer(sbox,wxVERTICAL);
	im_ImportFormat = new wxChoice(this,import_format,wxDefaultPosition,wxSize(120,-1),3,format_items);
	im_ImportFormat->SetSelection(0);
	wxStaticText *label4 = new wxStaticText(this,wxID_ANY,_("Delimiter"));
	im_ImportDelimiter = new wxChoice(this,wxID_ANY,wxDefaultPosition,wxSize(120,-1),2,delim_items);
	im_ImportDelimiter->SetSelection(0);
	im_ImportDelimiter->Enable(false);
	sbsizer->Add(im_ImportFormat,0,wxTOP|wxLEFT,0);
	sbsizer->Add(label4,0,wxTOP|wxLEFT,0);
	sbsizer->Add(im_ImportDelimiter,0,wxTOP|wxLEFT,0);

	wxStaticText *label2 = new wxStaticText(this,wxID_ANY,_("File path:"));
	im_ImpotFilePath = new wxTextCtrl(this, wxID_ANY, _T(""),wxDefaultPosition,wxSize(400,-1));
	im_FileRefBtn = new wxButton(this, import_fileref, _T("..."), wxPoint(-1,-1),wxSize(22,-1));

	wxStaticBox *sbox2 = new wxStaticBox(this,wxID_ANY,_("range of date"));
	wxStaticBoxSizer *sbsizer2 = new wxStaticBoxSizer(sbox2,wxVERTICAL);
	im_ImportUseRange = new wxCheckBox(this,import_userange, _("Use range of date"));
	im_ImportRangeBegin = new wxDatePickerCtrl(this,wxID_ANY,wxDefaultDateTime,wxDefaultPosition,wxSize(120,-1),wxDP_DEFAULT|wxDP_SHOWCENTURY|wxDP_DROPDOWN);
	wxStaticText *label3 = new wxStaticText(this,wxID_ANY,_("To"));
	im_ImportRangeEnd = new wxDatePickerCtrl(this,wxID_ANY,wxDefaultDateTime,wxDefaultPosition,wxSize(120,-1),wxDP_DEFAULT|wxDP_SHOWCENTURY|wxDP_DROPDOWN);
	im_ImportUseRange->Enable(false);
	im_ImportRangeBegin->Enable(false);
	im_ImportRangeEnd->Enable(false);
	sbsizer2->Add(im_ImportUseRange,0,wxTOP|wxLEFT,0);
		wxBoxSizer *sb2_child = new wxBoxSizer(wxHORIZONTAL);
		sb2_child->Add(im_ImportRangeBegin,0,wxTOP|wxLEFT,0);
		sb2_child->Add(label3,0,wxTOP|wxLEFT,5);
		sb2_child->Add(im_ImportRangeEnd,0,wxLEFT,5);
	sbsizer2->Add(sb2_child,0,wxTOP,5);

	im_ImportOptionArea = new wxStaticBox(this,wxID_ANY,_("Option"),wxDefaultPosition,wxSize(220,110));
	wxStaticBoxSizer *optarea = new wxStaticBoxSizer(im_ImportOptionArea,wxVERTICAL);
	im_ImOptReplace = new wxCheckBox(this,wxID_ANY, _("To replace current data with import data."));
	im_ImOptReplace->SetToolTip(_("To replace it with import data entirely if there is already data."));
	im_ImOptAddExpense = new wxCheckBox(this,wxID_ANY, _("To append and rebuild expense."));
	im_ImOptAddExpense->SetToolTip(_("When it add an expense, it merge with existing data and rebuild data."));
	optarea->Add(im_ImOptReplace,0,wxTOP|wxLEFT,0);
	optarea->Add(im_ImOptAddExpense,0,wxTOP|wxLEFT,0);
	optarea->Hide(1);

	im_ExecuteBtn = new wxButton(this, import_execute, _("Execute"), wxPoint(-1,-1),wxSize(85,-1));
	im_CloseBtn = new wxButton(this, import_close, _("Close"), wxPoint(-1,-1),wxSize(85,-1));
	
	im_ExecLog = new wxTextCtrl(this, wxID_ANY, _T(""),wxDefaultPosition,wxSize(-1,-1),wxTE_MULTILINE);
	im_ExecLogArea = new wxLogTextCtrl(im_ExecLog);
	wxLog::SetActiveTarget(im_ExecLogArea);

	wxBoxSizer *bsizer1 = new wxBoxSizer(wxVERTICAL);
		wxBoxSizer *bsizer2 = new wxBoxSizer(wxHORIZONTAL);
			wxBoxSizer *bsizer3 = new wxBoxSizer(wxVERTICAL);
				wxBoxSizer *bsizer6 = new wxBoxSizer(wxHORIZONTAL);
				bsizer6->Add(im_ImportType,0,wxTOP|wxLEFT,0);
				bsizer6->Add(sbsizer,1,wxLEFT|wxEXPAND,5);
				bsizer6->Add(sbsizer2,0,wxLEFT|wxEXPAND,5);
			bsizer3->Add(bsizer6,0,wxTOP|wxLEFT,0);
			bsizer3->Add(label1,0,wxTOP,5);
				wxBoxSizer *bsizer7 = new wxBoxSizer(wxHORIZONTAL);
				bsizer7->Add(im_ImportDBList,0,wxTOP|wxLEFT,0);
				bsizer7->Add(optarea,1,wxLEFT|wxEXPAND,5);
			bsizer3->Add(bsizer7,0,wxTOP|wxLEFT,0);
			//bsizer3->Add(im_ImportUseRange,0,wxTOP,5);
			//bsizer3->Add(im_ImportRangeBegin,0,wxTOP|wxLEFT,0);
			//bsizer3->Add(label3,0,wxTOP|wxLEFT,0);
			//bsizer3->Add(im_ImportRangeEnd,0,wxTOP|wxLEFT,0);
		bsizer2->Add(bsizer3,0,wxLEFT,5);
	bsizer1->Add(bsizer2,0,wxBOTTOM,5);
	bsizer1->Add(label2,0,wxTOP,5);
		wxBoxSizer *bsizer5 = new wxBoxSizer(wxHORIZONTAL);
		bsizer5->Add(im_ImpotFilePath,0,wxTOP|wxLEFT,0);
		bsizer5->Add(im_FileRefBtn,0,wxTOP|wxLEFT,0);
	bsizer1->Add(bsizer5,0,wxTOP|wxLEFT,0);
		wxBoxSizer *bsizer4 = new wxBoxSizer(wxHORIZONTAL);
		bsizer4->Add(im_ExecuteBtn,0,wxTOP|wxLEFT,0);
		bsizer4->Add(im_CloseBtn,0,wxLEFT,5);
	bsizer1->Add(bsizer4,0,wxRIGHT|wxALIGN_RIGHT,10);
	bsizer1->Add(im_ExecLog,0,wxTOP|wxLEFT,0);
	this->SetSizer(bsizer1);
	this->Layout();

}
void ImportDialog::im_CloseBtn_clicked(wxCommandEvent &event)
{
	this->EndModal(import_close);
	this->Hide();
}
void ImportDialog::im_ImportType_selected(wxCommandEvent &event)
{
	if (event.GetSelection() == 0) {
		im_ImportUseRange->Enable(false);
		im_ImportRangeBegin->Enable(false);
		im_ImportRangeEnd->Enable(false);
		im_ImOptReplace->Enable(true);
		im_ImOptAddExpense->Enable(true);
	}else{
		im_ImportUseRange->Enable(true);
		im_ImportRangeBegin->Enable(im_ImportUseRange->IsChecked());
		im_ImportRangeEnd->Enable(im_ImportUseRange->IsChecked());
		im_ImOptReplace->Enable(false);
		im_ImOptAddExpense->Enable(false);
	}
}
void ImportDialog::im_ImportUseRange_selected(wxCommandEvent &event)
{
	im_ImportRangeBegin->Enable(event.IsChecked());
	im_ImportRangeEnd->Enable(event.IsChecked());
}
void ImportDialog::im_FileRefBtn_clicked(wxCommandEvent &event)
{
	wxFileDialog *dialog;
	if (im_ImportType->GetSelection() == 0) {
		dialog = new wxFileDialog(this,_("Please choose a file for import."),_T(""),_T(""),
			_T("All file (*.*)|*.*;"));
	}else{
		dialog = new wxFileDialog(this,_("Please input file name for export."),_T(""),_T(""),
			_T("All file (*.*)|*.*"),wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
	}
	if (dialog->ShowModal() == wxID_OK) {
		im_ImpotFilePath->SetValue(dialog->GetPath());
	}
	dialog->Close(true);
}
void ImportDialog::im_ImportFormat_changed(wxCommandEvent &event)
{
	if (event.GetSelection() == 2) {
		im_ImportDelimiter->Enable(true);
	}else{
		im_ImportDelimiter->Enable(false);
	}
}
void ImportDialog::im_ExecuteBtn_clicked(wxCommandEvent &event)
{
	totalcnt = 0;
	successcnt = 0;
	failcnt = 0;
	if (im_ImportType->GetSelection() == 0) {
		ImportData(im_ImpotFilePath->GetValue());
		wxGetApp().SetChangeFlag(true);
	}else{
		int cnt = 0;
		for (int i = 0; i < (int)im_ImportDBList->GetCount(); i++) {
			if (im_ImportDBList->IsChecked(i)) {
				cnt++;
			}
		}
		if (cnt == 0) {
			wxMessageBox(_("No target is checked."),APPTITLE,wxOK|wxICON_EXCLAMATION,this);
			return;
		}
		ExportData(im_ImpotFilePath->GetValue());
	}
}
void ImportDialog::ExportData(const wxString &filepath)
{
	wxTextFile tfile;
	tfile.Create(filepath);
	tfile.Clear();
	switch (im_ImportFormat->GetSelection()) {
	case 0:
		wxLogMessage(_("Starting export as SQL-format..."));
		break;
	case 1:
		tfile.AddLine(_T("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"));
		tfile.AddLine(_T("<export>"));
		wxLogMessage(_("Starting export as XML-format..."));
		break;
	case 2:
		wxLogMessage(_("Starting export as CSV-format..."));
		break;
	}
	for (int i = 0; i < (int)im_ImportDBList->GetCount(); i++) {
		if (im_ImportDBList->IsChecked(i)) {
			TableDataFunction(tfile,i);
		}
	}
	if (im_ImportFormat->GetSelection() == 1) {
		tfile.AddLine(_T("</export>"));
	}
	tfile.Write();
	tfile.Close();
	wxLogMessage(_("Finish export."));
}
wxString ImportDialog::TableDataFunction(wxTextFile &tfile, int target)
{
	wxString sqltable[2],sqlorderby[2],sqlbetween[2],sqlcolumn[2],delim;
	if (im_ImportDelimiter->GetSelection() == 0) {
		delim = _T("\t");
	}else{
		delim = _T(",");
	}
	bool onheader = false;
	switch (target) {
	case 0://Budgets
		sqltable[0] = _T("db_budgets");
		sqltable[1] = _T("db_total");
		sqlorderby[0] = _T("bg_handle, bg_date");
		sqlorderby[1] = _T("tot_handle, tot_date");
		sqlbetween[0] = _T("where");
		sqlbetween[1] = _T("where");
		if (im_ImportUseRange->GetValue()) {
			sqlbetween[0] += StrF(_T(" bg_date between '%s' and '%s'"),
				im_ImportRangeBegin->GetValue().Format(DATEFORM).c_str(),
				im_ImportRangeEnd->GetValue().Format(DATEFORM).c_str());
			sqlbetween[1] += StrF(_T(" tot_date between '%s' and '%s'"),
				im_ImportRangeBegin->GetValue().Format(DATEFORM).c_str(),
				im_ImportRangeEnd->GetValue().Format(DATEFORM).c_str());
		}
		if (im_ImportType->GetSelection() == 2) {
			sqltable[0] = _T("db_budgets, db_expense, db_items, db_account");
			sqlcolumn[0] = _T("bg_date Date, bg_io IO, ac_name Account, ex_name Expense, it_name Items, bg_income Income, bg_expenditure Expenditure, bg_memo Memo");
			if (sqlbetween[0] == _T("where")) {
				sqlbetween[0] += _T(" bg_expense = ex_id and bg_items = it_id and ex_id = it_expense and bg_handle = ac_id ");
			}else{
				sqlbetween[0] += _T(" and bg_expense = ex_id and bg_items = it_id and ex_id = it_expense and bg_handle = ac_id ");
			}
			sqlbetween[1].Clear();
		}else{
			sqlcolumn[0] = _T("*");
			sqlbetween[0].Clear();
			sqlbetween[1].Clear();
		}
		sqlcolumn[1] = _T("*");
		wxLogMessage(_("Exporting budgets and total budgets..."));
		break;
	case 1://Expenses and Items
		sqlcolumn[0] = _T("*");
		sqlcolumn[1] = _T("*");
		sqltable[0] = _T("db_expense");
		sqltable[1] = _T("db_items");
		sqlorderby[0] = _T("ex_id");
		sqlorderby[1] = _T("it_id, it_expense");
		wxLogMessage(_("Exporting expenses and items..."));
		break;
	case 2://Accounts
		sqlcolumn[0] = _T("*");
		sqlcolumn[1] = _T("*");
		sqltable[0] = _T("db_account");
		sqltable[1].Clear();
		sqlorderby[0] = _T("ac_id");
		sqlorderby[1].Clear();
		wxLogMessage(_("Exporting accounts..."));
		break;
	case 3://Periodical budgets
		sqlcolumn[0] = _T("*");
		sqlcolumn[1] = _T("*");
		sqltable[0] = _T("db_period");
		sqltable[1].Clear();
		sqlorderby[0] = _T("pr_id");
		sqlorderby[1].Clear();
		wxLogMessage(_("Exporting periodical budgets..."));
		break;
	default:
		return wxEmptyString;
	}
	successcnt = 0;
	for (int i = 0; i < 2; i++) {
		wxString sql = StrF(_T("select %s from %s %s order by %s"),
			sqlcolumn[i].c_str(),sqltable[i].c_str(),sqlbetween[i].c_str(),sqlorderby[i].c_str());
		if (!sqltable[i].IsEmpty()) {
			onheader = false;
			wxSQLite3ResultSet rset =  wxGetApp().SQLDB().ExecuteQuery(sql);
			while (rset.NextRow()) {
				if (im_ImportFormat->GetSelection() == 1) {
					if (target == 0) {
						sqltable[i] = _T("db_budgets");
					}
					tfile.AddLine(StrF(_T("<%s>"),sqltable[i].c_str()));
				}else if (im_ImportFormat->GetSelection() == 2) {
					if (!onheader) {
						wxString line;
						for (int x = 0; x < rset.GetColumnCount(); x++) {
							line.Append(rset.GetColumnName(x));
							if (x < rset.GetColumnCount()-1) {
								line.Append(delim);
							}
						}
						tfile.AddLine(line);
						onheader = true;
					}
				}
				tfile.AddLine(generate_ExportFormat(sqltable[i],rset));
				if (im_ImportFormat->GetSelection() == 1) {
					if (target == 0) {
						sqltable[i] = _T("db_budgets");
					}
					tfile.AddLine(StrF(_T("</%s>"),sqltable[i].c_str()));
				}
				successcnt++;
			}
		}
	}
	wxLogMessage(StrF(_("Exported: %d"),successcnt));
	return wxEmptyString;
}
wxString ImportDialog::generate_ExportFormat(const wxString &table, wxSQLite3ResultSet &rset)
{
	wxString result, delim;
	int i = 0;
	switch (im_ImportFormat->GetSelection()) {
	case 0://SQL insert
		result = StrF(_T("insert into %s values("),table.c_str());
		for (i = 0; i < rset.GetColumnCount(); i++) {
			switch (rset.GetColumnType(i)) {
			case WXSQLITE_INTEGER:
				result.Append(rset.GetString(i).c_str());
				break;
			case WXSQLITE_TEXT:
				result.Append(_T("'"));
				result.Append(rset.GetString(i).c_str());
				result.Append(_T("'"));
				break;
			default:
				break;
			}
			if (i < rset.GetColumnCount()-1) {
				result.Append(_T(","));
			}
		}
		result.Append(_T(");"));
		break;
	case 1://XML
		for (i = 0; i < rset.GetColumnCount(); i++) {
			wxString strquo;
			switch (rset.GetColumnType(i)) {
			case WXSQLITE_INTEGER:
				strquo.Clear();
				break;
			case WXSQLITE_TEXT:
				strquo = _T("'");
				break;
			}
			wxString tag = StrF(_T("<%s>%s%s%s</%s>"),
				rset.GetColumnName(i).c_str(),
				strquo.c_str(),
				rset.GetString(i).c_str(),
				strquo.c_str(),
				rset.GetColumnName(i).c_str());
			result.Append(tag);
		}
		break;
	case 2://CSV
		if (im_ImportDelimiter->GetSelection() == 0) {
			delim = _T("\t");
		}else{
			delim = _T(",");
		}
		for (i = 0; i < rset.GetColumnCount(); i++) {
			switch (rset.GetColumnType(i)) {
			case WXSQLITE_INTEGER:
				result.Append(rset.GetString(i).c_str());
				break;
			case WXSQLITE_TEXT:
				result.Append(_T("'"));
				result.Append(rset.GetString(i).c_str());
				result.Append(_T("'"));
				break;
			default:
				break;
			}
			if (i < rset.GetColumnCount()-1) {
				result.Append(delim);
			}
		}
		break;
	}
	return result;
}
void ImportDialog::ImportData(const wxString &filepath)
{
	if (!wxFileExists(filepath)) {
		wxMessageBox(_("File not found!"),APPTITLE,wxOK|wxICON_EXCLAMATION,this);
		return;
	}
	if (im_ImOptReplace->GetValue()) {
		if (im_ImportDBList->IsChecked(0)) {
			wxGetApp().SQLDB().ExecuteUpdate(_T("delete from db_budgets"));
			wxGetApp().SQLDB().ExecuteUpdate(_T("delete from db_total"));
		}
		if (im_ImportDBList->IsChecked(1)) {
			wxGetApp().SQLDB().ExecuteUpdate(_T("delete from db_expense"));
			wxGetApp().SQLDB().ExecuteUpdate(_T("delete from db_items"));
		}
		if (im_ImportDBList->IsChecked(2)) {
			wxGetApp().SQLDB().ExecuteUpdate(_T("delete from db_account"));
		}
		if (im_ImportDBList->IsChecked(3)) {
			wxGetApp().SQLDB().ExecuteUpdate(_T("delete from db_period"));
		}
	}
	if (im_ImportFormat->GetSelection() == 1) {
		//XML
		wxXmlDocument tfile;
		if ((!tfile.Load(filepath)) || (tfile.GetRoot()->GetName() != _T("export"))) {
			wxMessageBox(_("This file is not just format!"),APPTITLE,wxOK|wxICON_EXCLAMATION,this);
			return;
		}
		wxXmlNode *child = tfile.GetRoot()->GetChildren();
		while (child) {
			if (ImportDataFunction(child)) {
				successcnt++;
			}else{
				failcnt++;
			}
			totalcnt++;
			child = child->GetNext();
		}
	}else if (im_ImportFormat->GetSelection() == 0) {
		//SQL insert
		wxTextFile tfile;
		if (!tfile.Open(filepath)) {
			wxMessageBox(_("This file is not just format!"),APPTITLE,wxOK|wxICON_EXCLAMATION,this);
			return;
		}
		for (wxString str = tfile.GetFirstLine(); !tfile.Eof(); str = tfile.GetNextLine()) {
			if (!str.IsEmpty()) {
				if (wxGetApp().SQLDB().CheckSyntax(str)) {
					try {
						if (wxGetApp().SQLDB().ExecuteUpdate(str) > 0) {
							successcnt++;
						}else{
							failcnt++;
						}
					}catch (wxSQLite3Exception err) {
						failcnt++;
					}
				}else{
					failcnt++;
				}
				totalcnt++;
			}
		}
	}else{
		wxMessageBox(_("The import is not possible with this format!"),APPTITLE,wxOK|wxICON_EXCLAMATION,this);
		return;
	}
	wxString endmes = StrF(_("Finish import.\nTotal: %d\nSuccess: %d\nFailed: %d"),
		totalcnt,successcnt,failcnt);
	//wxMessageBox(endmes,APPTITLE,wxOK|wxICON_INFORMATION,this);
	wxLogMessage(endmes.c_str());
}
bool ImportDialog::ImportDataFunction(wxXmlNode *tfile)
{
	wxXmlNode *child = tfile->GetChildren();
	wxArrayString name, arr;
	while (child) {
		arr.Add(child->GetNodeContent());
		name.Add(child->GetName());
		child = child->GetNext();
	}
	wxString sql = parse_ImportFormat(tfile->GetName(),name,arr);
	try {
		int ret = wxGetApp().SQLDB().ExecuteUpdate(sql);
		if (ret > 0) {
			return true;
		}else{
			wxLogMessage(StrF(_T("Invalid data: %s"),sql.c_str()));
			return false;
		}
	}catch (wxSQLite3Exception err) {
		wxLogMessage(err.GetMessage().c_str());
		wxString errdata;
		for (int i = 0; i < (int)name.GetCount(); i++) {
			errdata += StrF(_T("%s=%s\n"),name[i].c_str(),arr[i].c_str());
		}
		errdata.Append(_T("\n"));
		wxLogMessage(errdata.c_str());
		return false;
	}
}
wxString ImportDialog::parse_ImportFormat(const wxString &table, wxArrayString &name, wxArrayString &arr)
{
	wxString result[2];
	if (!wxGetApp().SQLDB().TableExists(table)) {
		return wxEmptyString;
	}
	result[0] = StrF(_T("insert into %s"),table.c_str());
	wxString sql = StrF(_T("select * from %s"),table.c_str());
	wxSQLite3ResultSet rset = wxGetApp().SQLDB().ExecuteQuery(sql);
	for (int i = 0; i < (int)name.GetCount(); i++) {
		if (rset.FindColumnIndex(name[i]) >= 0) {
			if (table == _T("db_budgets")) {
				//check current data, immport data really exists
				if (name[i] == _T("bg_expense")) {
					sql = StrF(_T("select count(*) from db_expense where ex_id = %s"),
						arr[i].c_str());
					if (wxGetApp().SQLDB().ExecuteScalar(sql) == 0) {
						return wxEmptyString;
					}
				}
				if (name[i] == _T("bg_items")) {
					sql = StrF(_T("select count(*) from bg_items where it_id = %s"),
						arr[i].c_str());
					if (wxGetApp().SQLDB().ExecuteScalar(sql) == 0) {
						return wxEmptyString;
					}
				}
				if (name[i] == _T("bg_handle")) {
					sql = StrF(_T("select count(*) from db_account where ac_id = %s"),
						arr[i].c_str());
					if (wxGetApp().SQLDB().ExecuteScalar(sql) == 0) {
						return wxEmptyString;
					}
				}
				if (name[i] == _T("bg_io")) {
					if ((arr[i] != _T("0")) && (arr[i] != _T("1"))) {
						return wxEmptyString;
					}
				}
			}
			//generate parts of name
			result[1].Append(name[i].c_str());
			if (i < (int)name.GetCount()-1) {
				result[1].Append(_T(","));
			}
			//generate parts of values
			result[2].Append(arr[i].c_str());
			if (i < (int)arr.GetCount()-1) {
				result[2].Append(_T(","));
			}
		}
	}
	return StrF(_T("%s(%s) values(%s)"),
		result[0].c_str(),result[1].c_str(),result[2].c_str());
}
void ImportDialog::Clear(void)
{
	im_ImportType->SetSelection(0);
	im_ImportFormat->SetSelection(0);
	for (int i = 0; i < (int)im_ImportDBList->GetCount(); i++) {
		im_ImportDBList->Check(i,false);
	}
	im_ImpotFilePath->Clear();
	im_ImportUseRange->SetValue(false);
	im_ImportRangeBegin->SetValue(wxDateTime::Now());
	im_ImportRangeEnd->SetValue(wxDateTime::Now());
	im_ImOptReplace->SetValue(false);
	im_ImOptAddExpense->SetValue(false);

	im_ImportUseRange->Enable(false);
	im_ImportRangeBegin->Enable(false);
	im_ImportRangeEnd->Enable(false);
	im_ImOptReplace->Enable(true);
	im_ImOptAddExpense->Enable(true);

	wxLog::SetActiveTarget(im_ExecLogArea);
}
